package com.example.w;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    //TextView speedtv;//定义显示速度的控件
    EditText editText;  //定义输入框控件
    private float x;
    private float y;
    private  float z;
   public final static OkHttpClient Ok=new OkHttpClient(); //调用OKHttp库,定义常量供整个项目用
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        init();
        //click();
        //click2();
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;

        });

    }
    private void init(){
       TextView button=findViewById(R.id.textView3);//获取发送控件
      // speedtv=findViewById(R.id.textView3);//获取速度控件

         editText=findViewById(R.id.editTextText2);//输入框控件

         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 sensor();
                 String str =input(x,y,z);

                 //获取传感器数据

                 sendData(str);
             }
         });
    }
    /** 获取用户输入的数据 */
    private String input(float x,float y,float z){

        String str=editText.getText().toString();
        return str +"-"+x+"-"+y+"-"+z;
        //Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
    }
    /** 获取传感器采集的数据*/
    private void sensor(){
        TextView tvx=findViewById(R.id.textView8);
        TextView tvy=findViewById(R.id.textView9);
        TextView tvz=findViewById(R.id.textView10);
        //获取内置传感器的管理器
        SensorManager sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
        if(sensorManager!=null){
            //获取加速传感器
            Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            //添加监听事件参数1传入事件，参数2传入传感器，参数3传入刷新频率
            sensorManager.registerListener(new SensorEventListener() {
                @Override
                public void onSensorChanged(SensorEvent event) {
                    //当传感器数值变化时，会执行本方法
                    if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {//如果类型是加速度传感器
                        x=event.values[0];     //获取x轴加速度值，水平方向加速度，平放值为0，左右移动值会变化，向左动是正值，向右动是负值，
                        y=event.values[1];     //获取y轴加速度值，垂直方向加速度，竖放值为0，上下移动值会变化，向上动是负值，向下动是正值，
                        z=event.values[2];     //获取z轴加速度值，重力加速度，正面朝上平放默认值为9.81，正面朝下平放默认值为-9.81，上下移动值会改变
                        tvx.setText("x:"+x);
                        tvy.setText("y:"+y);
                        tvz.setText("z:"+z);
                        input(x,y,z);
                    }
                }

                @Override
                public void onAccuracyChanged(Sensor sensor, int accuracy) {
                    //可以获取传感器
                    Toast.makeText(MainActivity.this,"精度："+accuracy,Toast.LENGTH_SHORT).show();

                }
            }, sensor, SensorManager.SENSOR_DELAY_UI);
        }
        else{
            Toast.makeText(MainActivity.this,"设备无传感器",Toast.LENGTH_SHORT).show();
        }

    }
    /** 提交数据给服务器*/
    private void  sendData(String str){


        //设置请求网址
        String url="http://121.41.58.41:9876/getWeb?data="+str;

        Request request=new Request.Builder().get().url(url).build();

        //异步请求网络
        Ok.newCall(request).enqueue(new Callback() {    //子线程
            @Override

            public void onFailure(Call call, IOException e) {
                //当网络不通时，执行本方法
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,"网络不通",Toast.LENGTH_SHORT).show();
                    }
                });
                //Toast.makeText(MainActivity.this,"网络不通",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //当网络正常时，请求服务器会执行本方法
                if(response.isSuccessful()){         //请求服务器成功

                    String data=response.body().string();
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,data,Toast.LENGTH_LONG).show();
                        }
                    });
                }
                else{                                //请求服务器失败
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"请求服务器失败",Toast.LENGTH_SHORT).show();
                        }
                    });
                    //Toast.makeText(MainActivity.this,"请求服务器失败",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}